package com.shangxia.monomer.common.util.CodeGenerator;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import lombok.extern.slf4j.Slf4j;

import static com.shangxia.monomer.common.content.SystemContent.PROJECT_PATH;

/**
 * @ProjectName: monomer
 * @ClassName: CodeGeneratorUtil
 * @Time: 2021/7/31
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
public class CodeGeneratorUtil {
    public static void main(String[] args) {

        //全局配置
        GlobalConfig globalConfig = new GlobalConfig.Builder()
                .author("shangxia")
                .enableSwagger()
                .dateType(DateType.ONLY_DATE)
                .openDir(false)
                .outputDir(PROJECT_PATH + "src/main/java")
                .fileOverride()
                .build();

        //数据库配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig.Builder(
                //驱动连接的URL
                "jdbc:mysql://127.0.0.1:3306/monomer?useUnicode= &useSSL=false&characterEncoding=utf8",
                //数据库账号
                "root",
                //数据库密码
                "root"
                //驱动名称
        ).build();

        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig.Builder().build()
                .controllerBuilder()
                .enableRestStyle().build()
                .serviceBuilder().build()
                .mapperBuilder()
                .enableBaseResultMap()
                .enableBaseColumnList().build()
                .entityBuilder().
                        enableLombok()
                .naming(NamingStrategy.underline_to_camel)
                .columnNaming(NamingStrategy.underline_to_camel)
                .enableSerialVersionUID()
                .enableTableFieldAnnotation()

//                .enableActiveRecord()
//                .enableColumnConstant()
//
                .build();

        //包相关的配置
        PackageConfig packageConfig = new PackageConfig.Builder("com.shangxia.monomer.business", "base").build();


        //自动生成器对象
        AutoGenerator autoGenerator = new AutoGenerator(dataSourceConfig)
                .strategy(strategyConfig)
                .packageInfo(packageConfig)
                .global(globalConfig);


        //运行自动生成器 选择模板 Freemarker
        autoGenerator.execute(new FreemarkerTemplateEngine());
    }

}
